import { useRoute,useRouter } from "vue-router";
import { RequestGoodsDetail } from '@/api/detail'
import { ref,type Ref,onMounted } from 'vue'
import type { IGoods } from "@/types/types"

export const useDetail = () =>{
    const route = useRoute();
    const id:any = route.query.id;
    const goods: Ref<IGoods> = ref({
        id: 0,
        shop: "",
        picture: "",
        product: '',
        price: 0,
        oldprice: 0,
        putaway: 0,
        detail: "",
        category_id: 0,
        list: []
    })
    // const goods:any = ref({})
    const GetGoodsDetail = async ()  =>{
        const data = await RequestGoodsDetail(id);
        const { resultCode,resultInfo } = data;
        if(resultCode === 1){
            goods.value = resultInfo
        }
    }
    onMounted(() =>{
        GetGoodsDetail()
    })
    return { goods }
}
export const useGoodsDetail = ()=>{
    const router = useRouter()
    // 跳转到商品详情页
    const bindGoodsDetail = (id:number)=>{
        router.push({path:'/detail',query:{id}})
    }
    return {bindGoodsDetail}
}